#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll k, l, mod;
ll s, w;
ll fastpow(ll b, ll p) {
  ll res = 1;
  b %= mod;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> k >> l >> mod;
  cin >> s >> w;
  if (s == 0) {
    if (l == 1)
      cout << k % mod;
    else if (l == 2)
      cout << k % mod * ((k - 1) % mod) % mod;
    else if (k < 3)
      cout << 0;
    else
      cout << k % mod * ((k - 1) % mod) % mod * fastpow(k - 2, l - 2) % mod;
  } else {
    if (l == 1)
      cout << 1;
    else if (l == 2)
      cout << (k - 1) % mod;
    else if (k < 3)
      cout << 0;
    else
      cout << ((k - 1) % mod) % mod * fastpow(k - 2, l - 2) % mod;
  }
  return 0;
}